#ifndef _TK_ScrCtrl_H_
#define _TK_ScrCtrl_H_
/*---------------------------------------------------------------------------------------------------------*/
//过零检测
#define RIO_H_Zero                      (P1 & 0x20)
#define RIO_L_Zero                      (!RIO_H_Zero)
#define RIO_R_Zero                      (P1 & 0x20)
//可控硅控制
#define Silicon_Open()                  (P22 = 0)   
#define Silicon_Close()                 (P22 = 1)   
/*---------------------------------------------------------------------------------------------------------*/
#define TRIGGER_TIME_US                 2000    // 触发脉冲时间（微秒）
#define TIMER_TICK_US                   125     // 定时器每 tick 时间（微秒）
#define TRIGGER_TIME                    (TRIGGER_TIME_US / TIMER_TICK_US)  // 计算出的触发时间
/*---------------------------------------------------------------------------------------------------------*/
#define Scrctrlmode     0                       //0--丢波控制 1--斩波控制
typedef struct {
    unsigned char LastScrPercent;
    unsigned char ScrCycleCnt;
    unsigned long ZeroPosition;
    unsigned int  SiliconOpenPulse;
} ScrCtrlContext;
extern ScrCtrlContext ScrCtrl;
/*---------------------------------------------------------------------------------------------------------*/
#if (Scrctrlmode == 0)
/*---------------------------------------------------------------------------------------------------------*/
#define N_CYCLE 50                            //100个完整的波为一个周期
extern unsigned char U8_ScrOpenArry[];
/*---------------------------------------------------------------------------------------------------------*/
#define  SCR_DRIVER      {                                               \
    if(ScrCtrl.LastScrPercent)                                           \
    {                                                                    \
        if(ScrCtrl.ZeroPosition != RIO_R_Zero)                           \
        {                                                                \
            ScrCtrl.ZeroPosition = RIO_R_Zero;                           \
            if(U8_ScrOpenArry[ScrCtrl.ScrCycleCnt])                      \
            {                                                            \
                ScrCtrl.SiliconOpenPulse = TRIGGER_TIME;                 \
            }                                                            \
            else                                                         \
            {                                                            \
                ScrCtrl.SiliconOpenPulse = 0;                            \
            }                                                            \
            if(ScrCtrl.ScrCycleCnt < (N_CYCLE - 1))                      \
            {                                                            \
                ScrCtrl.ScrCycleCnt++;                                   \
            }                                                            \
            else                                                         \
            {                                                            \
                ScrCtrl.ScrCycleCnt = 0;                                 \
            }                                                            \
        }                                                                \
        if(ScrCtrl.SiliconOpenPulse)                                     \
        {                                                                \
            ScrCtrl.SiliconOpenPulse--;                                  \
            Silicon_Open();                                              \
        }                                                                \
        else                                                             \
        {                                                                \
            Silicon_Close();                                             \
        }                                                                \
    }                                                                    \
    else                                                                 \
    {                                                                    \
        Silicon_Close();                                                 \
        ScrCtrl.ScrCycleCnt = 0;                                         \
    }                                                                    \
}                                                                        \
/*---------------------------------------------------------------------------------------------------------*/
#else
#define n_FREQUENCYY 50                         //交流电周期单位Hz
#define N_CYCLE      80                         //一次过零完成时中断的计数值
extern unsigned char U8_ScrDelayOpenTime;
/*---------------------------------------------------------------------------------------------------------*/
#define  SCR_DRIVER      {                                               \
    if(ScrCtrl.LastScrPercent)                                           \
    {                                                                    \
        if(ScrCtrl.ZeroPosition != RIO_R_Zero)                           \
        {                                                                \
            ScrCtrl.ZeroPosition = RIO_R_Zero;                           \
        }                                                                \
    }                                                                    \
}
#endif
/*---------------------------------------------------------------------------------------------------------*/
extern void SCR_Control(unsigned char percent);
/*---------------------------------------------------------------------------------------------------------*/
#endif 